package com.example.controller;

import com.example.pojo.PageBean;
import com.example.pojo.Result;
import com.example.pojo.User;
import com.example.service.UserService;
import com.example.utils.CheckUtil;
import com.example.utils.CryptoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Api(tags = "用户管理模块")
@Slf4j
@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("根据userID查询用户,路径参数")
    @GetMapping("/{userID}")
    public Result getById(@PathVariable Integer userID) {
        log.info("根据ID查询员工信息,userID:{}",userID);
        User user = userService.getById(userID);
        return Result.success(user);
    }
    @ApiOperation("根据UserID批量删除用户,路径参数，集合")
    @DeleteMapping("/{userIDs}")
    public Result delete(@PathVariable List<Integer> userIDs){
        log.info("批量删除操作,userIDs{}",userIDs);
        userService.delete(userIDs);
        return Result.success();
    }

    @ApiOperation("用户注册")
    @PostMapping
    public Result save(@RequestBody @Validated User user){
        log.info("新增员工,user:{}",user);
        return userService.save(user);
    }

    @ApiOperation("用户修改数据")
    @PutMapping
    public Result update(@ApiParam("根据userID找到并更新用户") @RequestBody User user){
        return userService.update(user);
    }

    @ApiOperation("用户修改密码数据")
    @PutMapping("/pwd")
    public Result updatePwd(@ApiParam("根据userID找到并更新用户") @RequestBody User user){
        return userService.updatePwd(user);
    }

    @ApiOperation("分页查询，其中姓名支持模糊查询,返回结果是最后修改时间降序")
    @GetMapping
    public Result page(@ApiParam("要查询第几页，默认是第一页") @RequestParam(defaultValue = "1") Integer page,
                       @ApiParam("每页要查询几条数据，默认是10")@RequestParam(defaultValue = "10") Integer pageSize,
                       @ApiParam("用户的真实名字，支持模糊查询")String name,
                       @ApiParam("用户性别，1-男性，2-女性") Short gender,
                       @ApiParam("用户类型，1-普通用户 2-管理员用户") Short userType,
                       @ApiParam("账号注册时间的起始范围 pattern = yyyy-MM-dd ") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @ApiParam("账号注册时间的截至范围 pattern = yyyy-MM-dd ") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("分页查询，参数：{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
        //调用service分页查询
        PageBean pageBean = userService.page(page,pageSize,name,gender,userType,begin,end);
        return Result.success(pageBean);

    }

    @ApiOperation("查询用户名是否存在")
    @GetMapping("/username/{userName}")
    public Result getById(@PathVariable("userName") String userName) {
        return Result.success(userService.isExistUserName(userName));
    }

}
